// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/**
 * @brief
 *
 * @version  0.1
 * @author   yuanchenxi
 * @date     2024.12.20
 */
#include "TABRND1Parser.h"

#include <algorithm>
#include <cstdint>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Table/ITABRND1Service.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (TABRND1);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/TABRND1/TABRND1.md>
*/
// 以下需要修改
enum TABRND1Index : std::uint8_t
{
    TABRND1Index_TID   = 1,
    TABRND1Index_XAIXS = 2,
    TABRND1Index_YAIXS = 3,

    TABRND1Index_ENTRIESLIST = 11,
};
SG::DataStructure::Common::Status BDF::BULKDATA::ParseTABRND1 (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                               std::vector<std::string>&                               dividedEntryInfo)
{
    // 初始化临时的TABLED5数据结构，用于存储解析后的数据
    SG::DataStructure::FEM::TABRND1Data tempTABRND1;

    // TODO(bug 001) m_dominId的正确填写
    tempTABRND1.m_domainId = 1;
    tempTABRND1.m_id       = convertToI64 (dividedEntryInfo[TABRND1Index_TID]);

    std::string tempXAXIS  = dividedEntryInfo[TABRND1Index_XAIXS];
    boost::trim (tempXAXIS);
    tempTABRND1.m_CODEX   = tempXAXIS == "Log" ? 0 : 1;

    std::string tempYAXIS = dividedEntryInfo[TABRND1Index_YAIXS];
    boost::trim (tempXAXIS);
    tempTABRND1.m_CODEY = tempXAXIS == "Log" ? 0 : 1;

    bool         parseEnd = false;
    const size_t rowNum   = dividedEntryInfo.size () / 10;
    for (size_t curRow = 1; curRow < rowNum; curRow++)
    {
        for (size_t fieldsColumn = 1; fieldsColumn < 9; fieldsColumn += 2)
        {
            // 计算当前字段的索引
            const size_t curIndex = curRow * 10 + fieldsColumn;
            std::string& F        = dividedEntryInfo[curIndex];
            std::string& G        = dividedEntryInfo[curIndex + 1];
            boost::trim (F);
            if (F == "ENDT")
            {
                parseEnd = true;
                break;
            }

            tempTABRND1.m_XYList.push_back ({ convertToReal (F), convertToReal (G) });
        }

        if (parseEnd)
        {
            break;
        }
    }

    // 将解析后的TABLED5数据添加到数据库服务中
    return dbServiceFactory->get<ITABRND1Service> ()->Add (tempTABRND1);
}